PBS_data[, M3_rekod := fcase(M3_rekod == 1,"Primary",
                             M3_rekod == 2, "Secondary",
                             M3_rekod == 3, "Higher"
)]

### Defining survey design 

srs_design <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data)

### Treatment

srs_treatment <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data[wariant == "Treatment"])

### Control 

srs_control <- svydesign(id=~nr_wyw, weights=~waga, data=PBS_data[wariant == "Control"])

### I5_P11_1_edu

I5_P11_1_edu_t <- as.data.table(svyby(~I5_P11_1, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_1_edu_t[, Status := "Treatment"]

I5_P11_1_edu_c <- as.data.table(svyby(~I5_P11_1, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_1_edu_c[, Status := "Control"]

I5_P11_1_edu <- rbind(I5_P11_1_edu_t, I5_P11_1_edu_c)

I5_P11_1_edu <- I5_P11_1_edu[, .(M3_rekod, I5_P11_1 = I5_P11_1*100, se = se*100, Status)]

I5_P11_1_edu_p  <- ggplot(I5_P11_1_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_1, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_1 - se, 
                    ymax = I5_P11_1 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') 

ggsave("I5_P11_1_edu_p.jpeg", device='jpeg', dpi=900)

### I5_P11_2_edu

I5_P11_2_edu_t <- as.data.table(svyby(~I5_P11_2, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_2_edu_t[, Status := "Treatment"]

I5_P11_2_edu_c <- as.data.table(svyby(~I5_P11_2, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_2_edu_c[, Status := "Control"]

I5_P11_2_edu <- rbind(I5_P11_2_edu_t, I5_P11_2_edu_c)

I5_P11_2_edu <- I5_P11_2_edu[, .(M3_rekod, I5_P11_2 = I5_P11_2*100, se = se*100, Status)]

I5_P11_2_edu_p <- ggplot(I5_P11_2_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_2, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_2 - se, 
                    ymax = I5_P11_2 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') + 
  scale_x_discrete(labels = c('Primary','Secondary','Higher'))

ggsave("I5_P11_2_edu_p.jpeg", device='jpeg', dpi=900)

### I5_P11_3_edu

I5_P11_3_edu_t <- as.data.table(svyby(~I5_P11_3, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_3_edu_t[, Status := "Treatment"]

I5_P11_3_edu_c <- as.data.table(svyby(~I5_P11_3, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_3_edu_c[, Status := "Control"]

I5_P11_3_edu <- rbind(I5_P11_3_edu_t, I5_P11_3_edu_c)

I5_P11_3_edu <- I5_P11_3_edu[, .(M3_rekod, I5_P11_3 = I5_P11_3*100, se = se*100, Status)]

I5_P11_3_edu_p <- ggplot(I5_P11_3_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_3, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_3 - se, 
                    ymax = I5_P11_3 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') + 
  scale_x_discrete(labels = c('Primary','Secondary','Higher'))

ggsave("I5_P11_3_edu_p.jpeg", device='jpeg', dpi=900)

### I5_P11_4_edu

I5_P11_4_edu_t <- as.data.table(svyby(~I5_P11_4, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_4_edu_t[, Status := "Treatment"]

I5_P11_4_edu_c <- as.data.table(svyby(~I5_P11_4, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_4_edu_c[, Status := "Control"]

I5_P11_4_edu <- rbind(I5_P11_4_edu_t, I5_P11_4_edu_c)

I5_P11_4_edu <- I5_P11_4_edu[, .(M3_rekod, I5_P11_4 = I5_P11_4*100, se = se*100, Status)]

I5_P11_4_edu_p <- ggplot(I5_P11_4_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_4, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_4 - se, 
                    ymax = I5_P11_4 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') + 
  scale_x_discrete(labels = c('Primary','Secondary','Higher'))

ggsave("I5_P11_4_edu_p.jpeg", device='jpeg', dpi=900)

### I5_P11_5_edu

I5_P11_5_edu_t <- as.data.table(svyby(~I5_P11_5, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_5_edu_t[, Status := "Treatment"]

I5_P11_5_edu_c <- as.data.table(svyby(~I5_P11_5, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_5_edu_c[, Status := "Control"]

I5_P11_5_edu <- rbind(I5_P11_5_edu_t, I5_P11_5_edu_c)

I5_P11_5_edu <- I5_P11_5_edu[, .(M3_rekod, I5_P11_5 = I5_P11_5*100, se = se*100, Status)]

I5_P11_5_edu_p <- ggplot(I5_P11_5_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_5, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_5 - se, 
                    ymax = I5_P11_5 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') + 
  scale_x_discrete(labels = c('Primary','Secondary','Higher'))

ggsave("I5_P11_5_edu_p.jpeg", device='jpeg', dpi=900)

### I5_P11_6_edu

I5_P11_6_edu_t <- as.data.table(svyby(~I5_P11_6, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_6_edu_t[, Status := "Treatment"]

I5_P11_6_edu_c <- as.data.table(svyby(~I5_P11_6, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_6_edu_c[, Status := "Control"]

I5_P11_6_edu <- rbind(I5_P11_6_edu_t, I5_P11_6_edu_c)

I5_P11_6_edu <- I5_P11_6_edu[, .(M3_rekod, I5_P11_6 = I5_P11_6*100, se = se*100, Status)]

I5_P11_6_edu_p <- ggplot(I5_P11_6_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_6, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_6 - se, 
                    ymax = I5_P11_6 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') + 
  scale_x_discrete(labels = c('Primary','Secondary','Higher'))

ggsave("I5_P11_6_edu_p.jpeg", device='jpeg', dpi=900)

### I5_P11_7_edu

I5_P11_7_edu_t <- as.data.table(svyby(~I5_P11_7, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_7_edu_t[, Status := "Treatment"]

I5_P11_7_edu_c <- as.data.table(svyby(~I5_P11_7, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_7_edu_c[, Status := "Control"]

I5_P11_7_edu <- rbind(I5_P11_7_edu_t, I5_P11_7_edu_c)

I5_P11_7_edu <- I5_P11_7_edu[, .(M3_rekod, I5_P11_7 = I5_P11_7*100, se = se*100, Status)]

I5_P11_7_edu_p <- ggplot(I5_P11_7_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_7, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_7 - se, 
                    ymax = I5_P11_7 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') + 
  scale_x_discrete(labels = c('Primary','Secondary','Higher'))

ggsave("I5_P11_7_edu_p.jpeg", device='jpeg', dpi=900)

### I5_P11_8_edu

I5_P11_8_edu_t <- as.data.table(svyby(~I5_P11_8, ~M3_rekod, srs_treatment, svymean, na = TRUE))

I5_P11_8_edu_t[, Status := "Treatment"]

I5_P11_8_edu_c <- as.data.table(svyby(~I5_P11_8, ~M3_rekod, srs_control, svymean, na = TRUE))

I5_P11_8_edu_c[, Status := "Control"]

I5_P11_8_edu <- rbind(I5_P11_8_edu_t, I5_P11_8_edu_c)

I5_P11_8_edu <- I5_P11_8_edu[, .(M3_rekod, I5_P11_8 = I5_P11_8*100, se = se*100, Status)]

I5_P11_8_edu_p <- ggplot(I5_P11_8_edu, aes(x = factor(M3_rekod, levels = c("Primary", "Secondary", "Higher")), y=I5_P11_8, fill = Status)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +
  geom_errorbar(aes(ymin = I5_P11_8 - se, 
                    ymax = I5_P11_8 + se), width=.2,
                position=position_dodge(.9)) +
  theme_light() + scale_fill_manual (values=c("dodgerblue4", "gold1", "darkgrey")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 20, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 20, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 20, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 20, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 20, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Level of education", y = "Share of respondets (%)", color='Treatment status') + 
  scale_x_discrete(labels = c('Primary','Secondary','Higher'))

ggsave("I5_P11_8_edu_p.jpeg", device='jpeg', dpi=900)


